package com.example.caoh.common.util;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

/**
 * @author wuxi
 */
public class ElsUtil {

	public static void formatSearchOptInQuery(ElsQuery query, JSONArray opts) {
		JSONObject o;
		JSONObject option;
		String key, value, select, radio;
		ElsMethod method, addMethod;
		ElsOp op;
		for (int i = 0, j = opts.size(); i < j; i++) {

			o = opts.getJSONObject(i);
			if (StringUtil.isBlank(o.get("option")))
				continue;
			key = o.getString("name");
			option = JSONObject.parseObject(o.getString("option"));
			radio = option.getString("radio_1");
			addMethod = getElsMethod(radio);
			ElsBool bool = new ElsBool();
			ElsBool bool1 = new ElsBool();
			select = option.getString("select_1");
			op = getElsOp(select);
			value = option.getString("input_1");
			Object qtype = o.get("qtype");
			if (select.equals("nk") || select.equals("ne"))
				method = ElsMethod.MustNot;
			else
				method = ElsMethod.Must;
			if (StringUtil.isNotBlank(value)) {
				if("bool".equals(qtype))
					value = (value.equals("是")?"12781001":"12781002");
				if (select.equals("lt") || select.equals("le") || select.equals("gt") || select.equals("ge")) {
					bool1.addRange(key, value, getElsRange(select), method);
				} else {
					if (op == ElsOp.Wildcard&&!"bool".equals(qtype))
						value = String.format("*%s*", value);
					bool1.add(method, op, key, value);
				}
			} else {
				if (op == ElsOp.Exists || op == ElsOp.Missing) {
					if(null!=o.get("qtype")){
						bool1.add(method, op, "field", key);
					}else{
						if(op == ElsOp.Exists){
							bool1.add(ElsMethod.MustNot, ElsOp.Term, key, "");
							bool1.add(ElsMethod.Must, ElsOp.Exists, "field", key);
						}else{
							bool1.add(ElsMethod.Should, ElsOp.Term,key, "");
							bool1.add(ElsMethod.Should, ElsOp.Missing, "field", key);
						}
						
					}
				}

			}
			bool.add(bool1, addMethod);

			select = option.getString("select_2");
			op = getElsOp(select);
			value = option.getString("input_2");
			if (select.equals("nk") || select.equals("ne"))
				method = ElsMethod.MustNot;
			else
				method = ElsMethod.Must;
			bool1 = new ElsBool();
			if (StringUtil.isNotBlank(option.get("input_2"))) {
				if (select.equals("lt") || select.equals("le") || select.equals("gt") || select.equals("ge")) {
					bool1.addRange(key, value, getElsRange(select), ElsMethod.Must);
				} else {
					op = getElsOp(select);
					if (op == ElsOp.Wildcard)
						value = String.format("*%s*", value);
					bool1.add(method, op, key, value);
				}
				bool.add(bool1, addMethod);
			} else {
				if (op == ElsOp.Exists || op == ElsOp.Missing) {
					bool1.add(method, op, "field", key);
					bool.add(bool1, addMethod);
				}
			}
			query.addQuery(bool, ElsMethod.Must);
		}
	}

	public static JSONObject buildAggs(String key,String value) {
		String json = " { \"dicts\": {\"terms\": {\"field\":\"%s\",\"size\":10000},\"aggs\": { \"names\": { \"top_hits\": { \"_source\": \"%s\", \"size\": 1}} }}}";
		return JSONObject.parseObject(String.format(json, key,value));
	}

	public static ElsRange getElsRange(String val) {
		switch (val) {
		case "lt":
			return ElsRange.Lt;
		case "le":
			return ElsRange.Lte;
		case "gt":
			return ElsRange.Gt;
		case "ge":
			return ElsRange.Gte;
		default:
			return null;
		}
	}

	public static ElsOp getElsOp(String select) {
		switch (select) {
		case "lk":
			return ElsOp.Wildcard;
		case "nk":
			return ElsOp.Wildcard;
		case "eq":
			return ElsOp.Term;
		case "ne":
			return ElsOp.Term;
		case "in":
			return ElsOp.Missing;
		case "nn":
			return ElsOp.Exists;
		default:
			return ElsOp.Term;
		}
	}

	public static ElsMethod getElsMethod(String method) {
		switch (method) {
		case "and":
			return ElsMethod.Must;
		case "or":
			return ElsMethod.Should;
		default:
			return null;
		}
	}
}
